extends State
class_name PlayerStateFall

# 重构后的玩家下落状态 - 简化逻辑，优化预跳跃

const PREJUMP_TIME: float = 0.15  # 预跳跃时间窗口

var prejump_timer: float = 0.0
var has_prejump_input: bool = false
var input_handler: InputHandler

func enter(_data: Dictionary = {}) -> void:
	# 获取输入处理器
	input_handler = character.get_node("InputHandler") as InputHandler
	if input_handler:
		input_handler.movement_input.connect(_on_movement_input)
		input_handler.jump_pressed.connect(_on_jump_pressed)
		input_handler.dash_pressed.connect(_on_dash_pressed)
	
	# 重置预跳跃状态
	prejump_timer = 0.0
	has_prejump_input = false
	
	animation.play("fall")

func exit() -> void:
	# 断开信号连接
	if animation.animation_finished.is_connected(change_state):
		animation.animation_finished.disconnect(change_state)
	
	if input_handler:
		if input_handler.movement_input.is_connected(_on_movement_input):
			input_handler.movement_input.disconnect(_on_movement_input)
		if input_handler.jump_pressed.is_connected(_on_jump_pressed):
			input_handler.jump_pressed.disconnect(_on_jump_pressed)
		if input_handler.dash_pressed.is_connected(_on_dash_pressed):
			input_handler.dash_pressed.disconnect(_on_dash_pressed)

func update(delta: float) -> void:
	_update_prejump(delta)
	_check_landing()

func physics_update(_delta: float) -> void:
	# 物理更新由移动输入处理
	pass

# 处理移动输入
func _on_movement_input(direction: Vector2) -> void:
	if not character.can_move:
		return
	
	# 空中移动控制
	var target_velocity = direction.x * character.speed
	character.velocity.x = move_toward(
		character.velocity.x, 
		target_velocity, 
		character.acceleation * get_physics_process_delta_time() * 0.7
	)

# 处理跳跃输入（预跳跃）
func _on_jump_pressed() -> void:
	if character.can_jump:
		character.velocity.y = -character.jump_speed
		character.can_jump = false
		input_handler.consume_buffered_input("jump")
		MusicPlayer.play_sfx("Jump")
		change_state("jump")
		return
	has_prejump_input = true
	prejump_timer = PREJUMP_TIME

# 处理冲刺输入
func _on_dash_pressed() -> void:
	if character.can_dash:
		change_state("dash")

# 更新预跳跃逻辑
func _update_prejump(delta: float) -> void:
	if has_prejump_input:
		prejump_timer -= delta
		
		# 如果着地了且还在预跳跃时间窗口内，执行跳跃
		if character.is_on_floor() and prejump_timer > 0:
			if character.current_dash_count < 1:
				character.restore_dash(1)
			character.velocity.y = -character.jump_speed
			character.can_jump = false
			input_handler.consume_buffered_input("jump")
			MusicPlayer.play_sfx("Jump")
			change_state("jump")
			return
		
		# 预跳跃时间窗口结束
		if prejump_timer <= 0:
			has_prejump_input = false

# 检查着陆
func _check_landing() -> void:
	if character.is_on_floor() and character.velocity.y >= 0:
		animation.play("land")
		if !animation.animation_finished.is_connected(change_state):
			animation.animation_finished.connect(change_state.bind("move").unbind(1))
		

# 获取状态数据
func get_state_data() -> Dictionary:
	return {
		"velocity": character.velocity,
		"has_prejump": has_prejump_input,
		"prejump_timer": prejump_timer,
		"is_falling": character.velocity.y > 0
	}

func _can_jump() -> bool:
	return character.can_jump && input_handler.has_buffered_input("jump")

## 调试信息
#func get_debug_info() -> String:
	#return "Fall State - Velocity: %s, PreJump: %s (%.2f)" % [
		#character.velocity,
		#has_prejump_input,
		#prejump_timer
	#]
